package com.cnvai.cms.web.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cnvai.cms.business.api.ChannelService;
import com.cnvai.cms.javabean.model.CmsChannel;
import com.cnvai.cms.web.vo.Datagrid;

@Controller
@RequestMapping("/channel")
public class ChannelController {

	@Inject
	private ChannelService channelService;

	public ChannelService getChannelService() {
		return channelService;
	}

	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}

	@RequestMapping("/channelHome")
	public String channel() {
		return "/channel/channel";
	}

	@RequestMapping("/channelAdd")
	public String channelAdd() {
		return "/channel/channelAdd";
	}

	@RequestMapping("/channelList")
	@ResponseBody
	public Datagrid getChannelList() {
		Datagrid data = new Datagrid();
		List<CmsChannel> list = getChannelService().getChannelListALL();
		data.setTotal(list.size());
		data.setRows(list);
		return data;
	}

	@RequestMapping("/saveChannel")
	public String saveChannel(CmsChannel channel) {
		String result = "success";
		try {
			getChannelService().save(channel);
		} catch (Exception e) {
			result = "fail";
		}
		return result;
	}

	@RequestMapping("/deleteChannel")
	@ResponseBody
	public String deleteChannel(String ids) {
		String result = "success";
		try {
			if (!getChannelService().deleteChannel(ids)) {
				result = "fail";
			}
		} catch (Exception e) {
			result = "fail";
		}
		return result;
	}
}
